package cn.com.shbs.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.shbs.Config.CommonUtil;
import cn.com.shbs.Config.Page;
import cn.com.shbs.Config.PasswordHelper;
import cn.com.shbs.Dao.SupplierInfoDao.SupplierInfoDao;
import cn.com.shbs.model.LoginUser;
import cn.com.shbs.model.Store;
import cn.com.shbs.model.Supplier;
import net.sf.json.JSONObject;

@RestController
@RequestMapping("API/")
public class SupplierInfoController {

	static Logger logger = LoggerFactory.getLogger(UserInfoController.class);

	@Autowired
	SupplierInfoDao supplierInfoDao;

	
	
	
	
	/**
	 * 供应商搜素
	 * @param request
	 * @param response
	 * @param supplier_name
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findSupplierInfoByName")
	public Object findSupplierInfoByName(HttpServletRequest request, HttpServletResponse response, 
			String supplier_name,
			Page page
			) throws Exception {

		logger.info("SupplierInfoController.findSupplierInfoByName()方法开始");

		String message = "success";
		JSONObject result = new JSONObject();

		HashMap map = new HashMap();
		List<Supplier> supplier_list = new ArrayList<Supplier>();
		try {
			
			
			int total=supplierInfoDao.findSupplierInfo_nums(supplier_name);
			page.setTotalCount(total);
			map.put("pagestart", page.getStartCount());
			map.put("limit",page.getPageSize());
			map.put("supplier_name",supplier_name);
			
			//查询供应商信息
			supplier_list=supplierInfoDao.findSupplierInfoByName(map);

			message = "success";

		} catch (Exception e) {
			logger.error("SupplierInfoController.findSupplierInfoByName()方法异常", e);
			message = "error";
			e.printStackTrace();
		}

		result.put("message", message);
		result.put("page", page);
		result.put("supplier_list", supplier_list);
		return result.toString();

	}
	
	
	/**
	 * 供应商信息修改
	 */
	@RequestMapping(value = "/UpSupplierInfoById")
	public Object UpSupplierInfoById(HttpServletRequest request, HttpServletResponse response,
			Supplier supplier
			) throws Exception {
		
		
		logger.info("ShopInfoController.UpdateShop()方法开始");
		
		String message = "success";
		JSONObject result=new JSONObject();
		

		try {
			
			supplierInfoDao.UpSupplierInfoById(supplier);
		
			message = "success";
			
		} catch (Exception e) {
			logger.error("ShopInfoController.UpdateShop()方法异常", e);
			message = "error";
			e.printStackTrace();
		}
		
		result.put("message",message);
	     return result.toString();

	}
	
	
	/**
	 * 根据ID查找供应商信息
	 * @param request
	 * @param response
	 * @param supplier_code
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/FindSupplierInfoById")
	public Object FindSupplierInfoById(HttpServletRequest request, HttpServletResponse response,
			String supplier_code
			) throws Exception {
		
		
		logger.info("ShopInfoController.FindSupplierInfoById()方法开始");
		
		String message = "success";
		JSONObject result=new JSONObject();
		
		List<Supplier> supplier_list=new ArrayList<Supplier>();
		Supplier supplier = new Supplier();
		
		try {
			
			supplier =	supplierInfoDao.FindSupplierInfoById(supplier_code);
		
			message = "success";
			
		} catch (Exception e) {
			logger.error("ShopInfoController.FindSupplierInfoById()方法异常", e);
			message = "error";
			e.printStackTrace();
		}
		
		result.put("message",message);
		result.put("Supplier",supplier);
	     return result.toString();

	}
	

}
